package modul.descptorUsability;

import ij.IJ;
import ij.ImagePlus;
import ij.process.ImageProcessor;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import descriptors.DescriptorFactory;
import descriptors.IDescriptor;

import modul.AModul;
import modulIOContainer.DefaultImageContainer;
import modulIOContainer.PartialDescriptedImage;
import modulIOContainer.Patch;

public class RegularDesciptorUsage extends AModul {

	public static final Logger logger = Logger.getLogger(RegularDesciptorUsage.class);
	
	public int GridWidth = 5;
	public int PatchWidth = 10;
	public int maxImageSize = 400;

	private IDescriptor descriptor;

	@Override
	public void initialize() {
		// TODO Auto-generated method stub

		// load descriptor
		this.descriptor = DescriptorFactory.getDescriptor("name");
	}

	@Override
	public Class<? extends DefaultImageContainer> getOutputClass() {
		return PartialDescriptedImage.class;
	}

	@Override
	public Class<DefaultImageContainer> getInputClass() {

		return DefaultImageContainer.class;
	}

	@Override
	public void process() {

		output= new ArrayList<DefaultImageContainer>(input.size());
		
		for (DefaultImageContainer di : input) {
			ImagePlus imp = IJ.openImage(di.getUri());
			imp=this.reziseImage(imp);			
			output.add(this.descriptImage(imp, di));
		}

	}

	public PartialDescriptedImage descriptImage(ImagePlus image,
			DefaultImageContainer org) {
		
		descriptor.setImage(image);
		
		int x = (image.getWidth() - PatchWidth) / GridWidth;
		int y = (image.getHeight() - PatchWidth) / GridWidth;

		PartialDescriptedImage newImage = new PartialDescriptedImage(org);

		newImage.patchList = new ArrayList<Patch>();

		// int i=0;
		for (int xi = 0; xi < x; xi++)
			for (int yi = 0; yi < y; yi++) {
				float posX = (xi * GridWidth + PatchWidth / 2.0f)
						/ image.getWidth();
				float posY = (yi * GridWidth + PatchWidth / 2.0f)
						/ image.getHeight();

				Patch p = descriptor.getPatch(posX, posY, PatchWidth);

				p.x = posX;
				p.y = posY;
				newImage.patchList.add(p);

				// System.out.println(i+ " "+
				// Arrays.toString(newImage.patches[xi * y + yi].attributes));
				// i++;
			}

		return newImage;
	}

	public ImagePlus reziseImage(ImagePlus imp) {
		
		ImagePlus newImage=imp;
		
		if (imp.getWidth() >= maxImageSize || imp.getHeight() >= maxImageSize) {
			int maxSize = Math.max(imp.getWidth(), imp.getHeight());

			ImageProcessor ip = imp.getProcessor();
			// ip.setInterpolationMethod(ImageProcessor.BILINEAR);
			ImageProcessor ip_small = ip.resize(maxImageSize * imp.getWidth()
					/ maxSize, maxImageSize * imp.getHeight() / maxSize);
			newImage = new ImagePlus("small", ip_small);
		}
		logger.info("test");
		return newImage;
		
	}

	public int getGridWidth() {
		return GridWidth;
	}

	public void setGridWidth(int gridWidth) {
		GridWidth = gridWidth;
	}

	public int getPatchWidth() {
		return PatchWidth;
	}

	public void setPatchWidth(int patchWidth) {
		PatchWidth = patchWidth;
	}

	public int getMaxImageSize() {
		return maxImageSize;
	}

	public void setMaxImageSize(int maxImageSize) {
		this.maxImageSize = maxImageSize;
	}

	public IDescriptor getDescriptor() {
		return descriptor;
	}

	public void setDescriptor(IDescriptor descriptor) {
		this.descriptor = descriptor;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		RegularDesciptorUsage reg= new RegularDesciptorUsage();
		reg.initialize();
		List<DefaultImageContainer> list =new ArrayList<DefaultImageContainer>() ;
		
		list.add(new DefaultImageContainer("D:\\Pictures\\ein Lemming.jpg"));
		
		reg.setInput(list);
		reg.process();
		System.out.println(((PartialDescriptedImage)reg.getOutput().get(0)).patchList.size());
		
	}

}
